//
//  MeatMapViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/8/1.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#define mBottomerH 220.f

#import "MeatMapViewController.h"
#import "MeatMapShopAddressViewController.h"


#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import <AMapFoundationKit/AMapFoundationKit.h>

#import "MeaetCityFriendHeader.h"
#import "MeatMapBottomer.h"




@interface MeatMapViewController ()<CLLocationManagerDelegate, MKMapViewDelegate>

@property (nonatomic, strong) MKMapView *mapView;
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLLocation *useLocation; //当前地址
@property (nonatomic, strong) UIImageView *bigPin; //大头针
@property (nonatomic, strong) UIButton *resetPositionIcon; //重新定位Icon

@property (nonatomic, strong) MeaetCityFriendHeader *bottomer_heaerView;
@property (nonatomic, strong) MeatMapBottomer *bottomer;


/**
 MeatSearchHeader 's LeftNavBtn -> UIButton
 */
@property (strong, nonatomic) UIButton *leftNavBtn;


@end

@implementation MeatMapViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setupUI];
    [self setupLayout];
}

- (void)setupUI{
    
    
    [self.view addSubview:self.mapView];
    [self.view addSubview:self.leftNavBtn];
    [self.view addSubview:self.bottomer_heaerView];
    [self.view addSubview:self.bottomer];

}

- (void)setupLayout{
    
    {
        _leftNavBtn.frame = CGRectMake(15, TJ_StatusBarHeight_Define, 44,  44);
    }
    
    {
        _mapView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight - mBottomerH);
        _resetPositionIcon.mj_y = _mapView.height - _resetPositionIcon.width - 35;
    }
    
    {
        _bottomer_heaerView.mj_y = _mapView.bottom -20;
    }
    
    {
        _bottomer.mj_y = _bottomer_heaerView.bottom;
    }

}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}


#pragma mark - private methods

//大头针跳动的动画
- (void)bigPinAnimation{
    
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        
        CGPoint center = self.bigPin.center;
        center.y -= 20;
        [self.bigPin setCenter:center];
        
    } completion:nil];
    
    [UIView animateWithDuration:0.45 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        
        CGPoint center = self.bigPin.center;
        center.y += 20;
        [self.bigPin setCenter:center];
        
    } completion:nil];
}


#pragma mark - getter methods

- (UIButton *)leftNavBtn{
    
    if (!_leftNavBtn) {
        _leftNavBtn = [UIButton makeButtonWithImageName:@"nav_back"];
        _leftNavBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        
        kWeakSelf(self);
        _leftNavBtn.tapBlock = ^{
            [weakself.navigationController popViewControllerAnimated:YES];
        };
    }
    
    return _leftNavBtn;
}


//添加地图控件
- (MKMapView *)mapView{
    
    if (!_mapView) {
        
        _mapView = [[MKMapView alloc]initWithFrame:CGRectZero];
        _mapView.delegate = self;
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        
        if(![CLLocationManager locationServicesEnabled]||[CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse){
            [_locationManager requestWhenInUseAuthorization];
        }
        _mapView.userTrackingMode = MKUserTrackingModeFollow;
        //_mapView.mapType = MKMapTypeStandard;
        
        //大头针
        [_mapView addSubview:self.bigPin];
        
        //重新定位Icon
        [_mapView addSubview:self.resetPositionIcon];
        
    }
    return _mapView;
}


//大头针
- (UIImageView *)bigPin
{
    if (!_bigPin) {
        
        _bigPin = [[UIImageView alloc] init];
        _bigPin.image = [UIImage imageNamed:@"address_location"];
        _bigPin.size = CGSizeMake(20, 40);
        _bigPin.mj_x = KScreenWidth/2.0 - 10;
        _bigPin.mj_y = (KScreenHeight - mBottomerH)/2.0 - 40;
    }
    return _bigPin;
}

//重新定位Icon
- (UIButton *)resetPositionIcon
{
    if (!_resetPositionIcon) {
        _resetPositionIcon = [UIButton buttonWithType:UIButtonTypeCustom];
        [_resetPositionIcon setImage:[UIImage imageNamed:@"meat_map_graylocation"] forState:UIControlStateNormal];
        _resetPositionIcon.size = CGSizeMake(32, 32);
        _resetPositionIcon.contentMode = UIViewContentModeCenter;
        _resetPositionIcon.mj_x = KScreenWidth - _resetPositionIcon.width - 15;
//        [_resetPositionIcon addTarget:self action:@selector(didChickedResetLocation) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _resetPositionIcon;
}


- (MeaetCityFriendHeader *)bottomer_heaerView{
    
    if (!_bottomer_heaerView) {
        _bottomer_heaerView = [[MeaetCityFriendHeader alloc] init];
        _bottomer_heaerView.titleLab.text = @"标注收货位置，确认是否在冷链配送范围内";
        _bottomer_heaerView.titleLab.textColor = UIColorFromRGB(0x969696);
    }
    return _bottomer_heaerView;
}


- (MeatMapBottomer *)bottomer{
    
    if (!_bottomer) {
        _bottomer = [[MeatMapBottomer alloc] init];
        
        @weakify(self);
        _bottomer.dealResetLocationBlock = ^{
            @strongify(self);

            
            
        };
        
        
        _bottomer.dealInputLocationBlock = ^{
            @strongify(self);

            MeatMapShopAddressViewController *vc = [[MeatMapShopAddressViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        };
        
        
        _bottomer.dealHistoryLocationBlock = ^(NSInteger params) {
            @strongify(self);

        };

    }
    return _bottomer;
}


@end
